<template>
  <div>
    <keep-alive>
      <div class="header-ct">
        <div class="header-logo">
          <img src="../../assets/img/logo2.png" alt="" />
        </div>
        <ul class="header-nav">
          <router-link v-for="(items, index) in menuList"
                       :to="items.path" @click="selectMenu(items.id)"
                       :class="defaultSelectPage === items.id ? 'active' : ''" :key="index">
            <li>{{ items.label }}</li>
            <span class="active-span"></span>
          </router-link>
        </ul>
        <div class="header-user">
          <div v-if="userInfo.token">
            <el-dropdown>
              <span class="el-dropdown-link">
                <img :src="userInfo.user.headPortrait" alt=""/>
                <i class="el-icon-arrow-down el-icon--right"></i>
              </span>
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item @click="$router.push('/studyCenter')">个人中心</el-dropdown-item>
                  <el-dropdown-item @click="logoutClick()">退出</el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
          </div>
          <div v-else>
            <el-button type="primary" round size="mini" @click="gologinClick()">登录</el-button>
            <el-button round size="mini" @click="goregister()">注册</el-button>
          </div>
        </div>
      </div>
    </keep-alive>
    <LoginBox />
    <RegisterBox />
<!--    <FormpassBox />-->
<!--    <FindpassBox />-->
  </div>
</template>

<script>
import { mapActions, mapState } from "vuex";
import LoginBox from "@/components/common/login-box.vue";
import RegisterBox from "@/components/common/register-box";

export default {
  name: "header-common",
  components: {
    LoginBox,
    RegisterBox,
  },
  data() {
    return {
      timer: null,
      dialogFormPassword: false,
      defaultSelectPage: 0,
      menuList: [
        { label: "首页", id: 1, path: "/homeCenter" },
        { label: "课程中心", id: 2, path: "/courseCenter" },
        { label: "学习中心", id: 3, path: "/studyCenter" },
        { label: "互动问答", id: 4, path: "/questionCenter" },
      ],
      loginForm: {
        stuId: "",
        password: "",
      },
      loginRules: {
        stuId: [{ required: true, trigger: "blur", message: "请输入学号" }],
        password: [{ required: true, trigger: "blur", message: "请输入密码" }],
      },
    };
  },

  watch: {
    $route: {
      handler(to, from) {
        let item = this.menuList.find((item) => {
          return item.path == to.path;
        });
        console.log(from)
        if (item && item.id) this.defaultSelectPage = item.id;
      },
      deep: true,
      immediate: true,
    },
  },

  computed: {
    ...mapState({ userInfo: (state) => state.user }),
  },

  mounted() {
  },
  methods: {
    ...mapActions("user", [ "logout"]),
    // 打开注册面板
    goregister() {
      this.$store.dispatch("dialog/setlogin", false);
      this.$store.dispatch("dialog/setregister", true);
    },
    // 打开登录面板
    gologinClick() {
      this.$store.dispatch("dialog/setlogin", true);
      this.$store.dispatch("dialog/setregister", false);
    },
    // 退出登录
    logoutClick() {
      this.logout();
    },
    // 选择菜单
    selectMenu(id) {
      this.defaultSelectPage = id;
    },
    // 忘记密码
    gofindpassword() {
      this.$store.dispatch("dialog/setfindpass", true);
    },
  },
};
</script>
<style lang="scss">
.el-dropdown-link {
  width: 50px;
  cursor: pointer;
  img {
    width: 32px;
    height: 32px;
    display: inline-block;
    vertical-align: middle;
  }
}
.header-ct {
  width: 1200px;
  height: 60px;
  margin: 0 auto;
  position: relative;
  letter-spacing: 2px;
  color: #9aabb8;
  line-height: 60px;
  .header-logo {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    height: 60px;
    font-size: 20px;
    font-weight: 700;
    padding-left: 15px;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
    margin-right: 180px;
    line-height: 60px;
    float: left;
    img {
      width: 152px;
      margin-top: 1px;
    }
  }
  .header-nav {
    float: left;
    height: 60px;
    padding-left: 10px;
    font-size: 16px;
    line-height: 60px;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
    li {
      width: 108px;
      text-align: center;
      float: left;
      position: relative;
      height: 60px;
      transition: all 0.3s;
      cursor: pointer;
      &.active {
        color: #d83b28;
        font-size: 18px;
        font-weight: 700;
        position: relative;
        .active-span {
          width: 2em;
        }
      }
      .active-span {
        width: 0px;
        height: 4px;
        display: block;
        border-radius: 4px;
        -webkit-transition: all 0.3s;
        transition: all 0.3s;
        position: absolute;
        z-index: 2;
        bottom: -4px;
        left: 50%;
        -webkit-transform: translateX(-50%);
        transform: translateX(-50%);

        // background: linear-gradient(90deg, #0059c5, #49d9e3);
        background: linear-gradient(
                to right,
                rgb(236, 72, 153),
                rgb(239, 68, 68),
                rgb(234, 179, 8)
        );
      }
    }
  }
  .header-user {
    height: 60px;
    float: right;
  }
}
.button-wjmm {
  margin: 15px 0;
  letter-spacing: 2px;
  text-align: right;
  font-size: 12px;
}
</style>

